﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace WindowsFormsApplication1.TransferCharacterRequests
{
    public class TransferCharacterManager
    {
        private List<TransferCharacterRequest> transferRequests = new List<TransferCharacterRequest>();

        public TransferCharacterManager() { }

        public bool SendTransferRequest(TransferCharacterRequest newRequest)
        {
            lock (transferRequests)
            {
                foreach (TransferCharacterRequest request in transferRequests)
                {
                    if (request.userID == newRequest.userID)
                        return false;
                }
                transferRequests.Add(newRequest);
            }

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("addtransferclient");
            xmldoc.WriteAttributeString("userid", newRequest.userID.ToString());
            xmldoc.WriteAttributeString("characterid", newRequest.characterID.ToString());
            xmldoc.WriteAttributeString("uid", newRequest.uid);
            xmldoc.WriteAttributeString("mapid", newRequest.MapID.ToString());
            xmldoc.WriteAttributeString("instance", newRequest.Instance.ToString());
            xmldoc.WriteAttributeString("mapx", newRequest.MapX.ToString());
            xmldoc.WriteAttributeString("mapy", newRequest.MapY.ToString());
            xmldoc.WriteEndElement();

            xmldoc.Flush();
            xmldoc.Close();

            stringWriter.Flush();

            string xmlMessage = stringWriter.ToString();

            if (SocketConnectionsManager.sendToServer(newRequest.transferToServer, xmlMessage) == false)
            {
                lock (transferRequests)
                {
                    transferRequests.Remove(newRequest);
                    return false;
                }
            }
            return true;
        }

        public void ProcessTransferCharacterResult(int userID, bool result)
        {
            lock (transferRequests)
            {
                foreach (TransferCharacterRequest transferRequest in transferRequests)
                {
                    if (transferRequest.userID == userID)
                    {
                        transferRequests.Remove(transferRequest);
                        transferRequest.processResult(result);
                        break;
                    }
                }
            }
        }
        //Used by TransferCharacterRequest timeout
        public void ProcessTransferCharacterResult(TransferCharacterRequest transferRequest, bool result)
        {
            lock (transferRequests)
            {
                transferRequests.Remove(transferRequest);
                transferRequest.processResult(result);
            }
        }
    }
}
